#ifndef __DIRECT_IO_H__
#define __DIRECT_IO_H__



#include "libpci/pci.h"
#include "debug.h"

int init_driver(); // returns 0 on error, 1 on success
void cleanup_driver(); // must be called after done using the driver

void* map_physical_addr_range( unsigned long phy_addr_start, 
						 unsigned long size ); // returns NULL on error, valid pointer on success
int unmap_physical_addr_range( void* virt_addr_start, 
		        unsigned long size ); // must be called after done with the mapped physical memory

unsigned long hal_pci_read_offset( unsigned long  bus, unsigned long  device,
		                   unsigned long  function, unsigned long  offset, 
				   unsigned char length);

void hal_pci_write_offset( unsigned long  bus, unsigned long  device, 
			   unsigned long  function, unsigned long  offset, 
			   unsigned long value, unsigned char length );

void outb(unsigned char value, unsigned short port);
void outw(unsigned short value, unsigned short port);
void outl(unsigned long value, unsigned short port);

unsigned char inb(unsigned short port);
unsigned short inw(unsigned short port);
unsigned long inl(unsigned short port);

//  port  start  ====================================================================================================
/*#define msg_pdbg DBG_INFO
#define msg_perr DBG_ERR
#define msg_pspew DBG_INFO
#define msg_cdbg  DBG_INFO
#define msg_cerr  DBG_ERR
#define msg_cspew  DBG_INFO
#define  msg_cwarn DBG_ERR
#define msg_cinfo  DBG_PROG
#define msg_cdbg2 DBG_INFO
#define msg_gdbg DBG_INFO
#define msg_gerr  DBG_ERR 
#define msg_gwarn DBG_ERR
#define msg_pwarn DBG_ERR

#define msg_ginfo DBG_PROG
#define msg_pinfo DBG_PROG
#define msg_cinfo DBG_PROG
#define msg_gdbg2 DBG_INFO
#define msg_pdbg2 DBG_INFO
#define msg_gspew DBG_INFO
#define msg_pspew DBG_INFO
#define msg_cspew DBG_INFO
#define print printf
*/
#define OUTB outb
#define OUTW outw
#define OUTL outl
#define INB  inb
#define INW  inw
#define INL  inl


#include<stdint.h>
#include<stdbool.h>


#define ERROR_PTR ((void*)-1)
#define MAP_FAILED	((void *) -1)
#define TIMEOUT_ERROR	-101

#define PHYSM_RW	0
#define PHYSM_RO	1
#define PHYSM_NOCLEANUP	0
#define PHYSM_CLEANUP	1
#define PHYSM_EXACT	0
#define PHYSM_ROUND	1

#define cpu_to_le8
#define cpu_to_le16
#define cpu_to_le32
#define cpu_to_le64

#define le_to_cpu8 cpu_to_le8
#define le_to_cpu16 cpu_to_le16
#define le_to_cpu32 cpu_to_le32
#define le_to_cpu64 cpu_to_le64


void *rphysmap(const char *descr, uintptr_t phys_addr, size_t len);
void physunmap(void *virt_addr, size_t len);

void mmio_writeb(uint8_t val, void *addr);
void mmio_writew(uint16_t val, void *addr);
void mmio_writel(uint32_t val, void *addr);
uint8_t mmio_readb(const void *addr);
uint16_t mmio_readw(const void *addr);
uint32_t mmio_readl(const void *addr);
void rmmio_writel(uint32_t val, void *addr);
void mmio_readn(const void *addr, uint8_t *buf, size_t len);

extern struct pci_access *pacc;	/* For board and chipset_enable */
/*struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
			      uint16_t card_vendor, uint16_t card_device);*/
// port end 
#endif //__DIRECT_IO_H__
